package de.gwtprime.client.widgets;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.places.ArticlePlace;
import de.gwtprime.client.util.AppUtils;
import de.gwtprime.shared.requestFactory.ArticleProxy;


public class ArticlePreviewWidgetImpl extends Composite{
	interface ArticlePreviewWidgetUiBinder extends UiBinder<Widget, ArticlePreviewWidgetImpl>{}
	ArticlePreviewWidgetUiBinder uiBinder = GWT.create(ArticlePreviewWidgetUiBinder.class);
	
	@UiField Anchor articleTitle;
	@UiField Label articleText;
	
	@UiField Image prevImage;
	//@UiField Label numberComments;
	public ArticlePreviewWidgetImpl(final ClientFactory clientFactory, final ArticleProxy article) {

		initWidget(uiBinder.createAndBindUi(this));
		articleTitle.setText(article.getTitle());
		articleText.setText(article.getText());
		
		prevImage.setUrl("img/"+ (AppUtils.isNullOrEmpty(article.getImage()) ? "preview_placeholder.png" :  article.getImage()));
		//numberComments.setText(String.valueOf(article.getNumberComments()));
		articleTitle.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				clientFactory.getPlaceController().goTo(new ArticlePlace(article.getId().toString()));
			}
		});
		
	}
	
	
	
}
